<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="讲师名字">
        <el-input v-model="teacher.name" placeholder="请输入名字"></el-input>
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="teacher.sort" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level" placeholder="请选择">
          <el-option label="高级讲师" :value="1"></el-option>
          <el-option label="首席讲师" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.intro"></el-input>
      </el-form-item>
      <el-form-item label="讲师头像">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:8221/admin/oss/file/upload?module=avatar"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :on-error="handleAvatarError"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="teacher.avatar" :src="teacher.avatar" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="讲师简历">
        <el-input
          v-model="teacher.career"
          :rows="10"
          type="textarea"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">确认提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import teacherApi from "@/api/teacher";
export default {
  data() {
    return {
      teacher: {}, //{"teacher":{name：}}
      id: "",
    };
  },
  created() {
    //路由出现id
    if (this.$route.params.id) {
      this.id = this.$route.params.id;
      this.fetchDataById(this.id);
    }
  },
  methods: {
    //头像上传前的校验
    beforeAvatarUpload(file) {
      const fileType = file.type;
      const fileSize = file.size;
      if (!fileType === "img/png") {
        this.$message.error("文件类型非法");
      }
      if (fileSize / 1024 / 1024 > 2) {
        ths.$message.error("上传文件过大");
      }
    },
    handleAvatarSuccess(response) {
      this.teacher.avatar = response.data.url;
      //强制刷新
      this.$forceUpdate();
    }, //上传失败
    handleAvatarError() {
      this.$message.error("上传失败，网络不稳定");
    },
   
    save() {
      //更新
      if (this.teacher.id) {
        teacherApi.updateTeacher(this.teacher).then((res) => {
          this.$message.success(res.message);
          if ((res.code = 20000)) {
            this.$router.push({ path: "/teacher" });
          }
        });
      }
      //新增
      else {
        teacherApi.saveTeacher(this.teacher).then((res) => {
          this.$message.success(res.message);
          if ((res.code = 20000)) {
            this.$router.push({ path: "/teacher" });
          }
        });
      }
    },
    fetchDataById(id) {
      teacherApi.getById(id).then((res) => {
        this.teacher = res.data.item;
      });
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>